package com.zm.zmgz.service;

import com.zm.zmgz.dao.ProjectStatisticsBeanMapper;
import com.zm.zmgz.pojo.ProjectProfitBean;
import com.zm.zmgz.pojo.ProjectStatisticsBean;
import com.zm.zmgz.pojo.ProjectStatisticsBeanExample;
import com.zm.zmgz.utils.BigDecimalUtils;
import com.zm.zmgz.utils.DateUtils;
import com.zm.zmgz.utils.StringCjwUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/05/27/17:55
 * @Description:项目统计表业务层
 */
@Service
public class ProjectStatisticsService {

    @Autowired
    ProjectProfitService projectProfitService;

    @Autowired
    ProjectStatisticsBeanMapper projectStatisticsBeanMapper;

    @Autowired
    BusinessAnalysisService businessAnalysisService;

    /***
     * @Description:通过批次id删除旧记录
     * 先
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 17:56:31
     */
    public int delteProjectStatisticsByBatch(Integer batchId) {
        ProjectStatisticsBeanExample projectStatisticsBeanExample = new ProjectStatisticsBeanExample();
        ProjectStatisticsBeanExample.Criteria criteria = projectStatisticsBeanExample.createCriteria();
        criteria.andBatchidEqualTo(batchId);
        return projectStatisticsBeanMapper.deleteByExample(projectStatisticsBeanExample);
    }


    /***
     * @Description: 根据批次id来创建项目统计记录
     * 通过批次id从营业分析表获取batch
     * 通过batch从项目盈利表中获取记录,统计处理生成记录
     * 创建一个ProjectStatistics集合
     * 遍历List<ProjectProfitBean>
     *  从ProjectStatistics集合中查询批次id是否存在;不存在旧新增;存在就获取(ProjectStatisticsBean)进行赋值
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 18:25:26
     */
    public int addProjectStatisticsByBatchId(Integer id) {
        // 通过批次id从营业分析表获取batch
        String batch = businessAnalysisService.getBatchById(id);
        // 通过batch从项目盈利表中获取记录,统计处理生成记录
        Date date = DateUtils.getDateByString(batch, "yyyy年MM月");
        List<ProjectProfitBean> list = projectProfitService.getBatchByDate(date, 0, 0);
        if (Objects.isNull(list) || list.size() == 0)
            return 0;
        // 创建一个ProjectStatistics集合
        List<ProjectStatisticsBean> list1 = new ArrayList<>();
        for (ProjectProfitBean profitBean : list) {
            // 该项目的记录是否已经创建了
            ProjectStatisticsBean projectStatisticsBean = getProjectProfitBeanByItemNameAndProjectProfitBeanList(profitBean.getItemname(), list1);
            // 不存在,就新建一个ProjectStatisticsBean
            if (Objects.isNull(projectStatisticsBean)) {
                projectStatisticsBean = new ProjectStatisticsBean();
                projectStatisticsBean.setBatchid(id); // 赋值批次id
                projectStatisticsBean.setItemname(profitBean.getItemname()); // 赋值项目名
            }
            // 对其它属性进行赋值
            projectStatisticsBean.setNumberofprojects(projectStatisticsBean.getNumberofprojects() + 1); // 项目人数+1
            // 判断该员工是否为试用期,如果是项目试用人数加1
            Boolean b = projectProfitService.isProbationByProfitBean(profitBean);
            if (b)
                projectStatisticsBean.setNumberoftrial(projectStatisticsBean.getNumberoftrial() + 1);
            // 判断该员工是否已经转正,如果是项目正式员工人数加1
            Boolean b2 = projectProfitService.isFormalByProfitBean(profitBean);
            if (b2)
                projectStatisticsBean.setNumberofpositive(projectStatisticsBean.getNumberofpositive() + 1);
            // 添加该员工的(税前工资+企业缴纳五险一金)到项目支出
            BigDecimal bigDecimal = projectStatisticsBean.getProjectexpenditure();
            String pretaxwages = profitBean.getPretaxwages();
            BigDecimal bigDecimal1 = BigDecimalUtils.getBigDecimalByString(pretaxwages); // 税前工资
            BigDecimal bigDecimal2 = profitBean.getEnterprisespayfiveinsurancesandonegold(); // 企业缴纳五险一金
            BigDecimal adds = BigDecimalUtils.adds(bigDecimal, bigDecimal1, bigDecimal2);// 之前 + 税前工资 + 企业缴纳五险一金
            projectStatisticsBean.setProjectexpenditure(adds);
            // 添加该员工的po结费(去除po税)到项目收入
            BigDecimal bigDecimal3 = projectStatisticsBean.getProjectincome();
            String posettlementfee = profitBean.getPosettlementfee();
            BigDecimal bigDecimal4 = BigDecimalUtils.getBigDecimalByString(posettlementfee); // po结费
            String taxrate = profitBean.getTaxrate();
            double percent = StringCjwUtils.getDoubleByPercent(taxrate); // po税
            BigDecimal multiply = bigDecimal4.multiply(new BigDecimal(1 - percent));// 去税 : po结费 * (1 - po税)
            BigDecimal add = BigDecimalUtils.add(bigDecimal3, multiply);
            projectStatisticsBean.setProjectincome(add);
            // 添加该员工的利润到项目利润中
            String profit = profitBean.getProfit();
            BigDecimal bigDecimal5 = BigDecimalUtils.getBigDecimalByString(profit);
            BigDecimal bigDecimal6 = projectStatisticsBean.getProjectprofit();
            BigDecimal bigDecimal7 = BigDecimalUtils.add(bigDecimal5, bigDecimal6);
            projectStatisticsBean.setProjectprofit(bigDecimal7);
            // 不存在,就添加到list中
            ProjectStatisticsBean projectStatisticsBean2 = getProjectProfitBeanByItemNameAndProjectProfitBeanList(profitBean.getItemname(), list1);
            if (Objects.isNull(projectStatisticsBean2))
                list1.add(projectStatisticsBean);
        }
        // 将集合添加到数据库中
        Integer i = insertProjectStatisticsBeans(list1);
        return i;
    }

    /***
     * @Description: 将ProjectStatisticsBean集合插入到项目统计表中
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-28 11:45:51
     */
    public Integer insertProjectStatisticsBeans(List<ProjectStatisticsBean> list1) {
        if (Objects.isNull(list1) || list1.size() == 0)
            return null;
        Integer integer = 0;
        for (ProjectStatisticsBean projectStatisticsBean : list1) {
            integer += projectStatisticsBeanMapper.insert(projectStatisticsBean);
        }
        return integer;
    }

    /***
     * @Description: 从一个ProjectStatisticsBean集合中获取itemname属性为传入值的ProjectStatisticsBean
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 18:44:41
     */
    private ProjectStatisticsBean getProjectProfitBeanByItemNameAndProjectProfitBeanList(String itemname, List<ProjectStatisticsBean> list) {
        if (Objects.isNull(list) || list.size() == 0)
            return null;
        for (ProjectStatisticsBean projectStatisticsBean : list) {
            if (projectStatisticsBean.getItemname().equals(itemname))
                return projectStatisticsBean;
        }
        return null;
    }

    /***
    * @Description: 根据批次id获取ProjectStatisticsBean集合
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-05-29 11:40:32
    */
    public List<ProjectStatisticsBean> getProjectStatisticsBeansByBatchId(Integer batchId) {
        ProjectStatisticsBeanExample projectStatisticsBeanExample = new ProjectStatisticsBeanExample();
        ProjectStatisticsBeanExample.Criteria criteria = projectStatisticsBeanExample.createCriteria();
        criteria.andBatchidEqualTo(batchId);
        List<ProjectStatisticsBean> list = projectStatisticsBeanMapper.selectByExample(projectStatisticsBeanExample);
        if(Objects.isNull(list) || list.size() == 0)
            return null;
        return list;
    }


    /***
    * @Description: 通过批次id获取该批次的项目总收入
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-06-02 17:05:57
    */
    public BigDecimal getTotalProjectIncomeByBatchId(Integer batchId) {
        if(Objects.isNull(batchId))
            return null;
        List<ProjectStatisticsBean> list = getProjectStatisticsBeansByBatchId(batchId);
        if(Objects.isNull(list) || list.size() == 0)
            return null;
        // 遍历集合,累加收入
        BigDecimal decimal = new BigDecimal(0);
        for (ProjectStatisticsBean bean : list) {
            BigDecimal decimal1 = bean.getProjectincome();
            BigDecimal decimal2 = BigDecimalUtils.nullToZero(decimal1);
            decimal = decimal.add(decimal2);
        }
        return decimal;
    }

    /***
     * @Description: 通过批次id获取该批次的项目总支出
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 17:05:57
     */
    public BigDecimal getTotalProjectExpenditureByBatchId(Integer batchId) {
        if(Objects.isNull(batchId))
            return null;
        List<ProjectStatisticsBean> list = getProjectStatisticsBeansByBatchId(batchId);
        if(Objects.isNull(list) || list.size() == 0)
            return null;
        // 遍历集合,累加支出
        BigDecimal decimal = new BigDecimal(0);
        for (ProjectStatisticsBean bean : list) {
            BigDecimal decimal1 = bean.getProjectexpenditure();
            BigDecimal decimal2 = BigDecimalUtils.nullToZero(decimal1);
            decimal = decimal.add(decimal2);
        }
        return decimal;
    }












}
